package com.bysj.sausys.activiti.listener;

import com.bysj.sausys.activiti.common.ActivitiUtil;
import com.bysj.sausys.model.SysAssCancel;
import com.bysj.sausys.model.SysUser;
import com.bysj.sausys.service.ISysAssCancelService;
import com.bysj.sausys.service.ISysUserService;
import com.bysj.sausys.util.SpringContextUtil;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;
import org.activiti.engine.runtime.ProcessInstance;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 社团注销复核监听器
 *
 * @author WangZK
 */
public class AssCancelReviewTaskListener implements TaskListener {
    @Override
    public void notify(DelegateTask delegateTask) {
        String processInstanceId = delegateTask.getProcessInstanceId();
        ProcessInstance processInstance = ActivitiUtil.getProcessInstance(processInstanceId);

        String key = processInstance.getBusinessKey();
        if (StringUtils.isNotBlank(key)) {
            ISysAssCancelService sysAssCancelService = SpringContextUtil.getBean("sysAssCancelService");
            SysAssCancel sysAssCancel = sysAssCancelService.findById(Integer.parseInt(key.split("\\.")[1]));

            ISysUserService sysUserService = SpringContextUtil.getBean("sysUserService");
            List<SysUser> list = sysUserService.findByAssId(sysAssCancel.getAssId());
            List<String> names = new ArrayList<>();
            if (list.size() > 0) {
                for (SysUser sysUser : list) {
                    names.add(sysUser.getUsername());
                }
            }

            delegateTask.addCandidateUsers(names);
        }
    }
}
